﻿/*
创建人：zhangrj
日期：2022-04-21
描述：班次维护页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ApsHandler/WorkShiftManageHandler.ashx", //后台处理url,
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    init: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        page.bind();
    },
    bind: function () {
        //班次列表加载
        $("#gdWorkShiftInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'WORKSHIFTNAME', title: '班次', width: 150 },
                 { field: 'STARTDATE', title: '开始时间', width: 120 },
                 { field: 'ENDDATE', title: '结束时间', width: 120 },
                 { field: 'ISUSENAME', title: '是否启用', width: 60 },
                 { field: 'ISUSE', hidden: true },
                 { field: 'NOTES', title: '备注', width: 300 },
                 { field: 'WORKSHIFTID', hidden: true }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.reloadPage(false, false, false, true, true);
                page.fillData(rowData);
            }
        });
        //列表加载
        $("#gdDetailInfo").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'STARTDATE', title: '开始时间', width: 120 },
                 { field: 'ENDDATE', title: '结束时间', width: 120 },
                 { field: 'SEQUENCE', title: '顺序号', width: 50 },
                 { field: 'MINS', title: '分钟', width: 80 },
                 { field: 'WORKSHIFTDETAILID', hidden: true }
                ]]
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                clearMessage();
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                page.reloadPage(false, true, true, true, true);
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });
        });
        //查询班次信息
        $("#btnSearch").on("click", function () {
            page.reloadPage(false, true, true, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //添加加工时间
        $("#btnAddTime").on("click", function () {
            page.reloadPage(false, false, false, false, true);
            page.addTime();
        });
        //删除加工时间
        $("#btnDeleTime").on("click", function () {
            page.reloadPage(false, false, false, false, true);
            page.deleTime();
        });
        //新建班次信息
        $("#btnAdd").on("click", function () {
            page.reloadPage(false, false, false, false, true);
            page.add();
        });
        //保存更新班次信息
        $("#btnUpdate").on("click", function () {
            page.reloadPage(false, false, false, false, true);
            page.update();
        });
        //删除班次信息
        $("#btnDele").on("click", function () {
            page.reloadPage(false, false, false, false, true);
            page.dele();
        });
        $(document).ready(function () {
            $('#txtStartTime').timespinner('setValue', '00:00:00');
            $('#txtEndTime').timespinner('setValue', '00:00:00');
        });
    },
    //查询
    search: function () {
        var workShiftName = $("#txtSearchName").textbox("getValue");
        var formData = {
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            action: "GetWorkShiftDatas",
            workShiftName: workShiftName
        };
        page.post(formData, function (res) {
            if (res.Result == false) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pagetotal = res.RowCount;
            $("#gdWorkShiftInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //添加班次信息
    add: function () {
        $('#btnAdd').linkbutton('disable'); //禁用按钮
        var resData = page.vaildData();
        if (resData.result === false) {
            displayMessage2(resData.msg, false, ["btnAdd"]);
            return;
        }
        resData.mains[0].TYPE = "add";
        var formData = { action: "SaveWorkShiftDatas", params: JSON.stringify(resData.mains), details: JSON.stringify(resData.details) };
        page.post(formData, function (res) {
            if (res.Result) { //成功后才将数据追加到列表中
                page.reloadPage(false, true, true, true, true);
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnAdd"]);
        });
    },
    //更新班次信息
    update: function () {
        $('#btnUpdate').linkbutton('disable'); //禁用按钮
        var row = $("#gdWorkShiftInfo").datagrid('getChecked'); //获取勾选的行
        if (row.length === 0) {
            displayMessage2("未选择班次", false, ["btnUpdate"]);
            return;
        }
        if (row.length > 1) {
            displayMessage2("只能选择一个班次维护", false, ["btnUpdate"]);
            return;
        }
        //验证填写数据
        var resData = page.vaildData();
        if (resData.result === false) {
            displayMessage2(resData.msg, false, ["btnUpdate"]);
            return;
        }
        resData.mains[0].WORKSHIFTID = row[0].WORKSHIFTID;
        resData.mains[0].TYPE = "update";
        var formData = { action: "SaveWorkShiftDatas", params: JSON.stringify(resData.mains), details: JSON.stringify(resData.details) };
        page.post(formData, function (res) {
            if (res.Result) {
                page.search();
                page.reloadPage(false, true, true, true, true);
            }
            displayMessage2(res.Message, res.Result, ["btnUpdate"]);
        });
    },
    //删除班次信息
    dele: function () {
        $('#btnDele').linkbutton('disable'); //禁用按钮
        var row = $("#gdWorkShiftInfo").datagrid('getChecked'); //获取勾选的行
        if (row.length === 0) {
            displayMessage2("未选择班次", false, ["btnDele"]);
            return;
        }
        var workShiftIds = "";
        for (var i = 0; i < row.length; i++) {
            workShiftIds = "'" + row[0].WORKSHIFTID + "',";
        }
        var params = [{ WORKSHIFTIDS: workShiftIds, TYPE: "delete"}];
        var formData = { action: "SaveWorkShiftDatas", params: JSON.stringify(params) };
        page.post(formData, function (res) {
            if (res.Result) {
                page.search();
                page.reloadPage(false, true, true, true, true);
            }
            displayMessage2(res.Message, res.Result, ["btnDele"]);
        });
    },
    //添加加工时间
    addTime: function () {
        var startTime = $("#txtStartTime").timespinner("getValue");
        if (isNullOrWhiteSpaceStr(startTime) || startTime === "00:00:00") {
            displayMessage("未填写开始时间", false);
            return;
        }
        var endTime = $("#txtEndTime").timespinner("getValue");
        if (isNullOrWhiteSpaceStr(endTime) || endTime === "00:00:00") {
            displayMessage("未填写结束时间", false);
            return;
        }
        if (startTime === endTime) {
            displayMessage("日期不能相同", false);
            return;
        }
        var rows = $("#gdDetailInfo").datagrid('getRows'); //获取所有加工行
        var formData = {
            action: "GetAddWorkShiftDetail",
            startTime: startTime,
            endTime: endTime,
            params: JSON.stringify(rows)
        };
        page.post(formData, function (res) {
            if (!res.Result) { //成功后才将数据追加到列表中
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdDetailInfo").datagrid('loadData', { rows: res.DBTable });
        });
    },
    //删除加工时间
    deleTime: function () {
        var rows = $("#gdDetailInfo").datagrid('getChecked'); //获取选中的行
        if (rows.length === 0) {
            displayMessage("未选择加工时间", false);
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            var index = $("#gdDetailInfo").datagrid('getRowIndex', rows[i]);
            $("#gdDetailInfo").datagrid('deleteRow', index);
        }
        $("#gdDetailInfo").datagrid('acceptChanges');
        var allRows = $("#gdDetailInfo").datagrid('getRows'); //获取所有行
        if (allRows.length > 0) {
            for (var i = 0; i < allRows.length; i++) {
                allRows[i].SEQUENCE = i + 1;
            }
            $("#gdDetailInfo").datagrid('loadData', { rows: allRows });
        }
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.url,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //验证填写信息并返回数据
    vaildData: function () {
        var resData = { msg: "", result: true };
        var workShiftName = $("#txtWorkShiftName").textbox("getValue");
        if (isNullOrWhiteSpaceStr(workShiftName)) {
            resData.result = false;
            resData.msg = "未填写班次名称";
            return resData;
        }
        var rows = $("#gdDetailInfo").datagrid('getRows'); //获取加工时间
        if (rows.length === 0) {
            resData.result = false;
            resData.msg = "未添加加工时间";
            return resData;
        }
        var isUse = $("#selIsUse").combobox("getValue");
        var isUseName = "是";
        if (isUse === "1") {
            isUseName = "否";
        }
        var notes = $("#txtRemark").textbox("getValue");
        resData.mains = [{
            WORKSHIFTNAME: workShiftName,
            ISUSE: isUse,
            STARTDATE: rows[0].STARTDATE,
            ENDDATE: rows[rows.length - 1].ENDDATE,
            ISUSENAME: isUseName,
            NOTES: notes
        }];
        resData.details = rows;
        return resData;
    },
    //重置页面（查询，结果，基础信息，加工区间，提示消息）
    reloadPage: function (query, result, base, time, msg) {
        if (query) {
            $("#txtSearchName").textbox("setValue", "");
        }
        if (result) {
            clearEasyuiDataGrid("gdWorkShiftInfo");
        }
        if (base) {
            $("#txtWorkShiftName").textbox("setValue", "");
            $("#txtRemark").textbox("setValue", "");
            $('#selIsUse').combobox('setValue', '0');
        }
        if (time) {
            clearEasyuiDataGrid("gdDetailInfo");
            $('#txtStartTime').timespinner('setValue', '00:00:00');
            $('#txtEndTime').timespinner('setValue', '00:00:00');
        }
        if (msg) {
            clearMessage();
        }
    },
    //填充数据
    fillData: function (row) {
        $("#txtWorkShiftName").textbox("setValue", row.WORKSHIFTNAME);
        $("#txtRemark").textbox("setValue", row.NOTES);
        $('#selIsUse').combobox('setValue', row.ISUSE);
        page.post({ action: "GetDetailInfo", workshiftId: row.WORKSHIFTID }, function (res) {
            if (!res.Result) { //成功后才将数据追加到列表中
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdDetailInfo").datagrid('loadData', { rows: res.DBTable });
        });
    }
};
page.init();
